#!/usr/bin/python
###############################################################################
#Programmers:                                                                 #
#             Danil Dotsenko, dd@accentsolution.com                           #
#Date: 07/25/2006                                                             #
#Filename: customdate.py                                                      #
#Version: 0.1                                                                 #
#                                                                             #
#                                                                             #
#       Copyleft 2006 Danil Dotsenko                                          #
#                                                                             #
#    This program is free software; you can redistribute it and/or modify     #
#    it under the terms of the GNU General Public License as published by     #
#    the Free Software Foundation; either version 2 of the License, or        #
#    (at your option) any later version.                                      #
#                                                                             #
#    This program is distributed in the hope that it will be useful,          #
#    but WITHOUT ANY WARRANTY; without even the implied warranty of           #
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the            #
#    GNU General Public License for more details.                             #
#                                                                             #
#    You should have received a copy of the GNU General Public License        #
#    along with this program; if not, write to the Free Software              #
#    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307  USA #
#                                                                             #
###############################################################################

'''These functions and classes are specifically tunned for ease of use and manupulation
with claendar-type applications.
The goal is to concentrate all time-retrieval functions here and
remove the need for importing and management of time-functions in the main code.'''

import datetime
from calendar import monthrange

class now:
	def __init__(self):
		'''
		This is a wrapper for many functions and vars related to today.
		.date
		.weekday
		.month
		.monthName
		.year
		.update()
		.isnewdate()
		'''
		self.date = 0
		self.weekday = 0
		self.month = 0
		self.monthName = None
		self.year = 0
		self.lastdate = None # will be used only for isnewdate() test function.
		# and this will assign all values.
		self.update()

	def update(self):
		'''Updates the date, weekday, month, monthName, year variables.'''
		current = datetime.date.today()
		self.date = int(current.strftime('%d'))
		weekday = int(current.strftime('%w'))-1 # by itself you get 0 - Sunday, 6 - saturday
		if weekday == -1:
			weekday = 6
		self.weekday = weekday # now 0-6 is Mon - Sun, like all normal people intended.
		self.month = int(current.strftime('%m'))
		self.monthName = current.strftime('%b') # %b - abbreviated name, %B - full name
		self.year = int(current.strftime('%Y'))

	def isnewdate(self):
		'''This function will compare the last stored .lastdate to current .date.
		Only this function changes .lastdate.'''
		self.update()
		if self.lastdate: # on first run, this value is still None, and there is nothing to compare to.
			if self.lastdate != self.date:
				self.lastdate = self.date
				return True
		else:
			self.lastdate = self.date
		return False

def weekday(year, month, date):
	'''Returns weekday number, where 0 = Sunday ... 6 = Saturday'''
	weekday = int(current.strftime('%w'))-1 # by itself you get 0 - Sunday, 6 - saturday
	if weekday == -1:
		weekday = 6
	return weekday # now 0-6 is Mon - Sun, like all normal people and calendar module intended.

def monthbeginswith(year, month):
	'''
	It returns 1st day's day of week
	Note, weekday starts with Monday at 0.
	'''
	dayofweek, trash = monthrange(year, month)
	return dayofweek

def monthlength(year, month):
	'''
	It returns the lendth of the month in days.
	'''
	trash, length = monthrange(year, month)
	return length